using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace BloodBot
{
    public class BloodCell : Microsoft.Xna.Framework.GameComponent
    {
        VertexPositionNormalTexture[] verts;
        Texture2D groundTex;
        int circSub;
        Vector3 radSize = new Vector3(10, 0, 0);
        float edgeHeight = 0.7f;
        Vector3 edgeThicknessb = new Vector3(1, 0, 0);
        Vector3 edgeThicknessc = new Vector3(1, 0, 0);
       //float edgeThickness = 1;
        Vector3 groundPos = new Vector3(0, -1, 0);
        Vector3 groundNormal = new Vector3(0, 1, 0);

        public BloodCell(Game game, Vector3 position, float radiusSize, int circumferenceSubdivisions, Texture2D texture)
            : base(game)
        {
            groundPos = position;
            circSub = circumferenceSubdivisions;
            radSize = new Vector3(radiusSize, 0, 0);
            groundTex = texture;
        }


        public VertexPositionNormalTexture[] buildGround()
        {
            Vector3 a = groundPos;
            Vector3 b = Vector3.Add(groundPos, radSize);
            Vector3 c = Vector3.Transform(b, Matrix.CreateRotationY(MathHelper.ToRadians(360 / circSub)));
            Vector3 d; Vector3 e; Vector3 f; Vector3 g; Vector3 h; Vector3 j;
            
            //figure out how many divisions in the circle there are, then build the connecting polys one after the other
            //until the circle is complete
            //Strip needs the origin, the previous point, and the rotated point)

            verts = new VertexPositionNormalTexture[circSub * 42];

            for (int i = 0; i <= circSub * 42 - 1; i++)
            {
                ////Perform rotation on edgeThicknessc which follows the circle divisions in the direction of c
                edgeThicknessc = Vector3.Transform(edgeThicknessc, Matrix.CreateRotationY(MathHelper.ToRadians(360 / circSub)));
                //inclining wall
                d = Vector3.Add(c, Vector3.Add(edgeThicknessc, new Vector3(0, edgeHeight, 0)));
                e = Vector3.Add(b, Vector3.Add(edgeThicknessb, new Vector3(0, edgeHeight, 0)));
                //top of wall
                f = Vector3.Add(d, edgeThicknessc);
                g = Vector3.Add(e, edgeThicknessb);
                //declining wall
                h = Vector3.Add(f, Vector3.Add(edgeThicknessc, new Vector3(0, -edgeHeight * 1.5f, 0)));
                j = Vector3.Add(g, Vector3.Add(edgeThicknessb, new Vector3(0, -edgeHeight * 1.5f, 0)));

                //origin
                verts[i] = new VertexPositionNormalTexture(a, groundNormal, new Vector2(0, 0));
                //point c
                verts[i + 1] = new VertexPositionNormalTexture(c, groundNormal, new Vector2(0, 1));
                //point b (rotated point c)
                verts[i + 2] = new VertexPositionNormalTexture(b, groundNormal, new Vector2(1, 0));
                //build the attached wall, which extrudes upwards from this edge BC to ED
                verts[i + 3] = new VertexPositionNormalTexture(b, groundNormal, new Vector2(0, 1));
                verts[i + 4] = new VertexPositionNormalTexture(c, groundNormal, new Vector2(1, 0));
                verts[i + 5] = new VertexPositionNormalTexture(d, groundNormal, new Vector2(0, 0));
                
                verts[i + 6] = new VertexPositionNormalTexture(b, groundNormal, new Vector2(0, 1));
                verts[i + 7] = new VertexPositionNormalTexture(d, groundNormal, new Vector2(1, 0));
                verts[i + 8] = new VertexPositionNormalTexture(e, groundNormal, new Vector2(0, 0));
                //build the top of the wall, which extrudes from the top edge ED to GF
                verts[i + 9] = new VertexPositionNormalTexture(e, groundNormal, new Vector2(0, 1));
                verts[i + 10] = new VertexPositionNormalTexture(d, groundNormal, new Vector2(1, 0));
                verts[i + 11] = new VertexPositionNormalTexture(f, groundNormal, new Vector2(0, 0));

                verts[i + 12] = new VertexPositionNormalTexture(e, groundNormal, new Vector2(0, 1));
                verts[i + 13] = new VertexPositionNormalTexture(f, groundNormal, new Vector2(1, 0));
                verts[i + 14] = new VertexPositionNormalTexture(g, groundNormal, new Vector2(0, 0));
                //build the outside edge of the wall, which will fall back to below the ground, this is so when we make the 
                //under side, the floor has thickness and there's no clipping. GF to JH
                verts[i + 15] = new VertexPositionNormalTexture(g, groundNormal, new Vector2(0, 1));
                verts[i + 16] = new VertexPositionNormalTexture(f, groundNormal, new Vector2(1, 0));
                verts[i + 17] = new VertexPositionNormalTexture(h, groundNormal, new Vector2(0, 0));

                verts[i + 18] = new VertexPositionNormalTexture(g, groundNormal, new Vector2(0, 1));
                verts[i + 19] = new VertexPositionNormalTexture(h, groundNormal, new Vector2(1, 0));
                verts[i + 20] = new VertexPositionNormalTexture(j, groundNormal, new Vector2(0, 0));
                //And the underside... Which is simply the reverse of what we've done so far. JH to -G-F
                verts[i + 21] = new VertexPositionNormalTexture(j, groundNormal, new Vector2(0, 1));
                verts[i + 22] = new VertexPositionNormalTexture(h, groundNormal, new Vector2(1, 0));
                verts[i + 23] = new VertexPositionNormalTexture(Vector3.Add(f, new Vector3(0, -edgeHeight*3f, 0)), groundNormal, new Vector2(0, 0));

                verts[i + 24] = new VertexPositionNormalTexture(j, groundNormal, new Vector2(0, 1));
                verts[i + 25] = new VertexPositionNormalTexture(Vector3.Add(f, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(1, 0));
                verts[i + 26] = new VertexPositionNormalTexture(Vector3.Add(g, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 0));
                //-G-F to -E-D
                verts[i + 27] = new VertexPositionNormalTexture(Vector3.Add(g, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 1));
                verts[i + 28] = new VertexPositionNormalTexture(Vector3.Add(f, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(1, 0));
                verts[i + 29] = new VertexPositionNormalTexture(Vector3.Add(d, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 0));

                verts[i + 30] = new VertexPositionNormalTexture(Vector3.Add(g, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 1));
                verts[i + 31] = new VertexPositionNormalTexture(Vector3.Add(d, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(1, 0));
                verts[i + 32] = new VertexPositionNormalTexture(Vector3.Add(e, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 0));
                //-E-D to -B-C
                verts[i + 33] = new VertexPositionNormalTexture(Vector3.Add(e, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 1));
                verts[i + 34] = new VertexPositionNormalTexture(Vector3.Add(d, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(1, 0));
                verts[i + 35] = new VertexPositionNormalTexture(Vector3.Add(c, new Vector3(0, -edgeHeight, 0)), groundNormal, new Vector2(0, 0));

                verts[i + 36] = new VertexPositionNormalTexture(Vector3.Add(e, new Vector3(0, -edgeHeight * 3f, 0)), groundNormal, new Vector2(0, 1));
                verts[i + 37] = new VertexPositionNormalTexture(Vector3.Add(c, new Vector3(0, -edgeHeight, 0)), groundNormal, new Vector2(1, 0));
                verts[i + 38] = new VertexPositionNormalTexture(Vector3.Add(b, new Vector3(0, -edgeHeight, 0)), groundNormal, new Vector2(0, 0));
                //And finally the last tri -B-C-A
                verts[i + 39] = new VertexPositionNormalTexture(Vector3.Add(b, new Vector3(0, -edgeHeight, 0)), groundNormal, new Vector2(0, 1));
                verts[i + 40] = new VertexPositionNormalTexture(Vector3.Add(c, new Vector3(0, -edgeHeight, 0)), groundNormal, new Vector2(1, 0));
                verts[i + 41] = new VertexPositionNormalTexture(Vector3.Add(a, new Vector3(0, -edgeHeight, 0)), groundNormal, new Vector2(0, 0));
               
                //Perform rotation on point b
                b = Vector3.Transform(b, Matrix.CreateRotationY(MathHelper.ToRadians(360 / circSub)));
                //Perform rotation on point b again to create point c
                c = Vector3.Transform(b, Matrix.CreateRotationY(MathHelper.ToRadians(360 / circSub)));
                //Perform rotation on edgeThicknessb which follows the circle divisions in the direction of b
                edgeThicknessb = Vector3.Transform(edgeThicknessb, Matrix.CreateRotationY(MathHelper.ToRadians(360 / circSub)));
                //next 41 points
                i += 41;
            }

            return verts;
        }

        public int getVertsToDraw()
        {
            return circSub*14;
        }

        public override void Initialize()
        {

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

    }
}
